<template lang="pug">
    kalix-dialog.user-add(title='个人信息修改' bizKey="user" ref="kalixBizDialog" v-bind:form-model.sync="formModel" v-bind:targetURL="targetURL" v-bind:submitBefore="submitBefore")
        div.el-form(slot="dialogFormSlot")
            <!--el-form-item(label="用户类型" prop="userType" v-bind:rules="rules.userType" v-bind:label-width="labelWidth")-->
                <!--kalix-dict-select(v-model="formModel.userType" appName="admin" dictType="用户类型" v-on:input="change($event)" disabled)-->
            el-form-item(label="登录名" prop="loginName" v-bind:rules="rules.loginName" v-bind:label-width="labelWidth")
                el-input(v-model="formModel.loginName")
            el-form-item(label="头像" prop="icon" v-bind:label-width="labelWidth")
                kalix-upload(v-model="formModel.icon" v-bind:isImage="true" style="width:100%")
            el-form-item(label="姓名" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
                el-input(v-model="formModel.name")
            el-form-item.short(label="身份标识" prop="identity" v-bind:label-width="labelWidth" v-bind:rules="rules.identity")
              el-select(v-model="formModel.identity" placeholder="请选择" v-on:input="changeW($event)" style="width:100%" )
                el-option(v-for="item in level" v-bind:key="item.value" v-bind:label="item.label" v-bind:value="item.value")
            <!--el-form-item.short(v-if="identityStatus" label="是否首席专家" prop="whetherChief" v-bind:label-width="labelWidth" v-bind:rules="rules.whetherChief")-->
              <!--el-select(v-model="formModel.whetherChief" placeholder="请选择" style="width:100%")-->
                <!--el-option(v-for="item in isYoN" v-bind:key="item.value" v-bind:label="item.label" v-bind:value="item.value")-->
            el-form-item(label="科室" prop="department" v-bind:rules="rules.department" v-bind:label-width="labelWidth")
                el-input(v-model="formModel.department")
            el-form-item(label="职称" prop="title" v-bind:rules="rules.title" v-bind:label-width="labelWidth")
                el-input(v-model="formModel.title")
            el-form-item(label="详细简介" prop="information" v-bind:rules="rules.information" v-bind:label-width="labelWidth")
                el-input(v-model="formModel.information")
            el-form-item.short(label="所属医院id" prop="hospitalId" v-bind:label-width="labelWidth" v-bind:rules="rules.hospitalId" class="none" )
                el-input(v-model="formModel.hospitalId" )
            el-form-item.short(label="所属医院" prop="hospital" v-bind:label-width="labelWidth" v-bind:rules="rules.hospital" )
              el-select(v-model="formModel.hospitalId" filterable placeholder="请选择所属医院" @change="getUid" style="width:100%")
                el-option(v-for="item in hName" v-bind:key="item.id" v-bind:label="item.hospitalName" v-bind:value="item.id")
            el-form-item.short(label="医院LOGO" prop="hospitalLogo" v-bind:label-width="labelWidth" v-bind:rules="rules.hospitalLogo" )
                img(:src="formModel.hospitalLogo" width="200"  height="200")
            el-form-item.short(label="分类id" prop="cid" v-bind:label-width="labelWidth" v-bind:rules="rules.cid" class="none" )
              el-input(v-model="formModel.cid" )
            el-form-item.short(label="分类" prop="className" v-bind:label-width="labelWidth" v-bind:rules="rules.className" )
              el-select(v-model="formModel.cid" filterable placeholder="请选择分类" @change="getCid" style="width:100%")
                el-option(v-for="item in cName" v-bind:key="item.id" v-bind:label="item.className" v-bind:value="item.id")
            el-form-item(label="个人签章" prop="personalseal" v-bind:rules="rules.personalseal" v-bind:label-width="labelWidth")
                kalix-upload(v-model="formModel.personalseal" v-bind:isImage="true" style="width:100%")
            el-form-item(label="单位签章" prop="unitseal" v-bind:rules="rules.unitseal" v-bind:label-width="labelWidth")
                kalix-upload(v-model="formModel.unitseal" v-bind:isImage="true" style="width:100%")
            template(v-if="formModel.userType === 0 || formModel.userType === 2")
                el-form-item(label="工号" prop="code" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.code")
                el-form-item(label="岗位" prop="position" v-bind:label-width="labelWidth")
                    kalix-dict-select(v-model="formModel.position" appName="admin" dictType="岗位名称" style="width:100%")
                el-form-item(label="姓名" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.name")
                el-form-item(label="性别" prop="sex" v-bind:label-width="labelWidth")
                    el-radio-group(v-model="formModel.sex")
                        el-radio(label="男")
                        el-radio(label="女")
                el-form-item(label="邮箱" prop="email" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.email")
                kalix-phone-form-item(v-model="formModel.phone" v-bind:label-width="labelWidth")
                kalix-mobile-form-item(v-model="formModel.mobile" v-bind:label-width="labelWidth")
            template(v-else-if="formModel.userType === 1")
                el-form-item(label="学号" prop="code" v-bind:rules="rules.code_xh" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.code")
                el-form-item(label="姓名" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.name")
                el-form-item(label="性别" prop="sex" v-bind:label-width="labelWidth")
                    el-radio-group(v-model="formModel.sex")
                        el-radio(label="男")
                        el-radio(label="女")
                el-form-item(label="邮箱" prop="email" v-bind:rules="rules.email" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.email")
                kalix-phone-form-item(v-model="formModel.phone" v-bind:label-width="labelWidth")
                kalix-mobile-form-item(v-model="formModel.mobile" required v-bind:label-width="labelWidth")
            template(v-else-if="formModel.userType === 3")
                el-form-item(label="企业组织机构代码" prop="code" v-bind:rules="rules.code_qy" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.code")
                el-form-item(label="公司名称" prop="name" v-bind:rules="rules.name" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.name")
                el-form-item(label="邮箱" prop="email" v-bind:rules="rules.email" v-bind:label-width="labelWidth")
                    el-input(v-model="formModel.email")
                kalix-phone-form-item(v-model="formModel.phone" required v-bind:label-width="labelWidth")
                kalix-mobile-form-item(v-model="formModel.mobile" v-bind:label-width="labelWidth")
            template(v-else)
                // 暂无
</template>

<script type="text/ecmascript-6">
    import FormModel from './model'
    import {userURL} from '../../config.toml'

    export default {
      name: 'AdminUserEdit',
      data() {
        // let validateLoginName = (rule, value, callback) => {
        //   if (value === '') {
        //     callback(new Error('请输入登录名'))
        //   } else {
        //     this.axios.request({
        //       method: 'GET',
        //       url: userURL + '/loginname/' + value,
        //       data: '',
        //       params: {}
        //     }).then(response => {
        //       if (response.data) {
        //         callback(new Error('该登录名已被使用'))
        //       } else {
        //         callback()
        //       }
        //     }).catch(() => {
        //     })
        //   }
        // }
        return {
          hName: [],
          cName: [],
          hLogo: [],
          formModel: Object.assign({}, FormModel),
          rules: {
            userType: [{type: 'number', required: false, message: '请选择用户类别', trigger: 'change'}],
            code_xh: [{required: true, message: '请输入学生学号', trigger: 'blur'}],
            code_qy: [{required: true, message: '请输入企业组织机构代码', trigger: 'blur'}],
            name: [{required: true, message: '请输入名称', trigger: 'blur'}],
            department: [{required: true, message: '请输入科室', trigger: 'blur'}],
            title: [{required: true, message: '请输入职称', trigger: 'blur'}],
            hospitalName: [{required: true, message: '请选择所属医院', trigger: 'blur'}],
            className: [{required: true, message: '请选择分类', trigger: 'blur'}],
            personalseal: [{required: true, message: '请上传个人签章', trigger: 'blur'}],
            unitseal: [{required: true, message: '请上传单位签章', trigger: 'blur'}],
            identity: [{required: true, message: '请选择身份标识', trigger: 'blur'}],
            // loginName: [{required: true, message: '请输入登录名', trigger: 'blur'}],
            loginName: [{required: false, trigger: 'blur'}],
            information: [{required: true, validator: '请输入详细简介', trigger: 'blur'}],
            email: [
              {required: true, message: '请输入邮箱地址', trigger: 'blur'},
              {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}
            ]
          },
          targetURL: userURL,
          labelWidth: '140px',
          identityStatus: null,
          level: [{
            value: '医生',
            label: '医生'
          }, {
            value: '专家',
            label: '专家'
          }, {
            value: '管理专家',
            label: '管理专家'
          }, {
            value: '超级管理员',
            label: '超级管理员'
          }],
          isYoN: [{
            value: '是',
            label: '是'
          }, {
            value: '否',
            label: '否'
          }]
        }
      },
      mounted() {
        this.gethospitalName()
        this.getclassName()
        this.gethospitalLogo()
      },
      methods: {
        submitBefore(baseDialog, callBack) {
          console.log('this.formModel.password========', this.formModel.password)
          this.formModel.password = null
          callBack()
        },
        getCid(val) {
          let obj = {}
          obj = this.cName.find((item) => {
            return item.id === val
          })
          this.formModel.className = obj.className
        },
        getUid(val) {
          let obj = {}
          obj = this.hName.find((item) => {
            return item.id === val
          })
          this.formModel.hospital = obj.hospitalName
          this.getLogo(val)
        },
        getLogo(val) {
          console.log('val----', val)
          let obj = {}
          obj = this.hLogo.find((item) => {
            return item.id === val
          })
          this.formModel.hospitalLogo = obj.hospitalLogo
        },
        gethospitalName() {
          this.axios.request({
            method: 'GET',
            url: '/camel/rest/telemedicine/hospitals'
          }).then(res => {
            this.hName = res.data.data
          })
        },
        gethospitalLogo() {
          this.axios.request({
            method: 'GET',
            url: '/camel/rest/telemedicine/hospitals'
          }).then(res => {
            this.hLogo = res.data.data
          })
        },
        getclassName() {
          this.axios.request({
            method: 'GET',
            url: '/camel/rest/telemedicine/classs'
          }).then(res => {
            this.cName = res.data.data
          })
        },
        changeW(item) {
          if (item === '专家') {
            this.identityStatus = true
          } else if (item === '医生') {
            this.identityStatus = false
            this.formModel.whetherChief = '否'
          } else if (item === '管理专家') {
            this.identityStatus = true
          } else {
            this.identityStatus = false
            this.formModel.whetherChief = '否'
          }
        },
        open(obj) {
          this.$refs.kalixBizDialog.open('', true, obj)
        },
        change(event) {
          this.$refs.kalixBizDialog.$refs.dialogForm.clearValidate()
        }
      }
    }
</script>
<style scoped lang="stylus" type="text/stylus">
  .none
    display none
</style>
